package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.client.enums.TriggerStrategy;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.CheckHtTimeSXAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
@AttrHandler(CheckHtTimeSXAttrHandler.class)
public class CheckHtTimeSXAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (TriggerStrategy.LOAD_VALUE.equals(data.getTriggerStrategy())) {
            return;
        }
        //业务开始结束时间校验
        //210008278 开始时间
        //210008279 结束时间

        if (KiteStringUtils.isEqual("210008278", attr.getFieldName())) { //服务起始时间
            KiteAttr endTimeAttr = inst.getAttr("210008279");
            if (null!=endTimeAttr&&KiteStringUtils.isNotEmpty(attr.getValue()) && KiteStringUtils.isNotEmpty(endTimeAttr.getValue())) {
                Date startDate = DateFormatUtils.parseStrToDate(attr.getValue());
                Date endDate = DateFormatUtils.parseStrToDate(endTimeAttr.getValue());
                if (startDate.after(endDate)) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("业务开始时间不得晚于结束时间");
                }
            }
        }

        if (KiteStringUtils.isEqual("210008279", attr.getFieldName())) { //服务截止时间
            KiteAttr startTimeAttr = inst.getAttr("210008278");
            if (null!=startTimeAttr&&KiteStringUtils.isNotEmpty(attr.getValue()) && KiteStringUtils.isNotEmpty(startTimeAttr.getValue())) {
                Date startDate = DateFormatUtils.parseStrToDate(startTimeAttr.getValue());
                Date endDate = DateFormatUtils.parseStrToDate(attr.getValue());
                if (startDate.after(endDate)) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("业务结束时间必须大于开始时间");
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
